package com.ericsson.resourceplanner.utils;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import com.ericsson.resourceplanner.exceptions.ResourcePlannerException;

/**
 * @author earnmaj
 *
 */
public class CommonUtils {
	/*public static final SimpleDateFormat DATE_FORMAT_INPUT= new SimpleDateFormat(ResourcePlannerConstants.DATE_FORMAT_INPUT);
	public static final SimpleDateFormat DATE_FORMAT_OUTPUT= new SimpleDateFormat(ResourcePlannerConstants.DATE_FORMAT_OUTPUT);
	
	*/
	private static Map<String, SimpleDateFormat> dateFormatters=new HashMap<String, SimpleDateFormat>();
	
	/*public  static final Workbook ConvertToExcel(String format, Map<String, Object[]> dataMap) throws ResourcePlannerException{
		Workbook workbook = null;
		FileOutputStream fos=null;
		String fileName=ResourcePlannerConstants.EXCEL_REPORT_FIRSTNAME+ResourcePlannerConstants.EXCEL_REPORT_EXTENSION_XLS;
		try {
			workbook= new HSSFWorkbook();
			Sheet sheet=workbook.createSheet("Export Results");
			fos= new FileOutputStream(fileName);
			workbook.write(fos);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			throw new ResourcePlannerException("Exception While Creating Workbook", e);
		} catch (IOException e) {
			throw new ResourcePlannerException("Exception While Creating Workbook", e);
		}finally{
			if(fos !=null){
				try {
					fos.close();
				} catch (IOException e) {
					throw new ResourcePlannerException("Exception While Creating Workbook", e);
					
				}
			}
			
			if(workbook !=null){
				try {
					workbook.close();
				} catch (IOException e) {
					throw new ResourcePlannerException("Exception While Creating Workbook", e);
					
				}
			}
		}
		
		return workbook;
	}*/
	
	/*formats and returns the string representation based on the  output pattern specified*/
	
	public static final Date getFormattedDateFromString(String pattern, String dateString){
	
		SimpleDateFormat inFormatter=null;
		Date formattedDate=null;
		
		if(dateString == null || dateString.length()==0){
			throw new ResourcePlannerException("input date is null or blank");
		}
		if(pattern!=null && pattern.length()>0){
			inFormatter=getFormatter(pattern);
		}
		
		try {
			formattedDate = inFormatter.parse(dateString);
					
		} catch (ParseException e) {
			throw new ResourcePlannerException("ParseException while parsing input Date" +dateString + ":: "+ pattern  + ":: " ,e);
		}
		
		return formattedDate;
		
	}
	
	
	public static final SimpleDateFormat getFormatter(String pattern){
		if(dateFormatters.get(pattern)!=null){
			return dateFormatters.get(pattern);
		}else{
			SimpleDateFormat format = new SimpleDateFormat(pattern);
			dateFormatters.put(pattern, format);
			return format;
		}
		
	}
	
	public static final Map<String, Object> ConvertObjectToMap(Object obj) throws ResourcePlannerException{
        /*Class<?> pomclass = obj.getClass();
        pomclass = obj.getClass();*/
        java.lang.reflect.Method[] methods = obj.getClass().getMethods();


        Map<String, Object> map = new TreeMap<String, Object>();
        for (java.lang.reflect.Method m : methods) {
           if (m.getName().startsWith("get") && !m.getName().startsWith("getClass")) {
              Object value;
			try {
				value = (Object) m.invoke(obj);
			} catch (IllegalArgumentException e) {
				throw new ResourcePlannerException("IllegalArgumentException while Invoking", e);
			} catch (IllegalAccessException e) {
				throw new ResourcePlannerException("IllegalAccessException while Invoking", e);
			} catch (InvocationTargetException e) {
				throw new ResourcePlannerException("InvocationTargetException while Invoking", e);
			}
              map.put(m.getName().substring(3), (Object) value);
           }
        }
    return map;
}
	
}




